package com.cloud.cloudshop.shop.controller.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cloud.cloudshop.shop.redisdao.RedisUserGoodsRepository;
import com.cloud.common.entity.GoodsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
public class RedisTemplateTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisUserGoodsRepository redisUserGoodsRepository;


    @RequestMapping("/test")
    public void test(){

        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setId("234234");
        //测试redis
        List<GoodsEntity> listGoods = new ArrayList<>();
        listGoods.add(goodsEntity);
        stringRedisTemplate.opsForValue().set("list", JSON.toJSONString(listGoods), 100, TimeUnit.SECONDS);
        String list222 = stringRedisTemplate.opsForValue().get("list");
        List<GoodsEntity> userPhotos1 = JSONArray.parseArray(list222, GoodsEntity.class);
        redisUserGoodsRepository.save(goodsEntity);
        List<GoodsEntity> all = redisUserGoodsRepository.findAll();
        stringRedisTemplate.opsForValue().set("my", "me", 10, TimeUnit.MINUTES);
    }



}
